<?php

ob_start();

error_reporting(E_ALL);

define('AJAX_DIR', dirname(dirname(dirname(__FILE__))) . '/');
define('AJAX_INIT', AJAX_DIR . 'include/init.inc.php');
define('AJAX_FUNCTIONS', AJAX_DIR . 'include/modules/');

if (!isset($_REQUEST['intelajax']['function']) || !isset($_REQUEST['intelajax']['module']))
{
	exit('Need to load function and module.');	
}

if (!file_exists(AJAX_INIT))
{
	exit("Cannot load init file: " . AJAX_INIT);
}

define('AJAX_CALL', AJAX_FUNCTIONS . '' . $_REQUEST['intelajax']['module'] . '/functions/Ajax.functions.php');

if (!file_exists(AJAX_CALL))
{
	exit("Cannot load function: " . AJAX_CALL);
}

require_once AJAX_INIT;
require_once AJAX_CALL;

$aModules = array
(
	'Chat',
	'Account',
	'Ads'
);

$aFunctions = array
(
	'deleteChatSession',
	'deleteSessions',
	'deleteFromRoom',
	'getRandomAds'
);

if (!in_array($_REQUEST['intelajax']['function'], $aFunctions) || !in_array($_REQUEST['intelajax']['module'], $aModules))
{
	exit('Invalid AJAX Call');
}

$sReturn = (isset($_REQUEST['intelajax']['params']) ? call_user_func_array($_REQUEST['intelajax']['function'], $_REQUEST['intelajax']['params']) : call_user_func($_REQUEST['intelajax']['function']));

if (App::getParam('debug'))
{
	print_r($_REQUEST);

	$sContent = ob_get_contents();
	
	ob_clean();

	$hOpen = fopen(AJAX_DIR . 'file/cache/ajax.log', 'a');
	fwrite($hOpen, $sContent . "\n" . $sReturn);
	fclose($hOpen);
}

ob_clean();

if ($sReturn)
{
	// $sReturn = str_replace(array("\n", "\t"), '', $sReturn);
	header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
	header("Cache-Control: no-cache, must-revalidate");
	header("Pragma: no-cache");
	header("Content-Type: text/javascript");
	echo $sReturn;
}

ob_end_flush();

?>